
#include "main.h"

#ifdef   SOUND_MODE
  
volatile near bit RingSound;	
volatile near bit Beep;	
volatile near bit BeepError;
volatile near bit Beep_SMS;
//volatile unsigned int near count3 = 0;//BEEP_LONG; 

//volatile near unsigned int Beep_Long;

void Sound_Init ( void )
{
	Beep = 0;
	BeepError = 0;
	Beep_SMS = 0;
	RingSound = 0;
	TMR2 = 0;
	T2CON = 0b01111010;		// prescaler 1:16
	PR2 = 0;
	CCPR5 = 300;
	CCP5CON = 0x00;		//Compare mode
	TMR2IF = 0;
	TMR2IE = 0;	
	TRISG3 = 0;
	TRISG4 = 0;	
	CCP5IE = 0;
	CCP5IF = 0;
	TMR2ON = 0;						  

	TMR3 = 0;
	T3CON = 0xC0;
	CCP4CON = 0;//0x02;	// Compare mode RG3 will be toggle
	CCPR4 = SOUND_TONE;
	TMR3ON = 1;
	CCP4IE = 1;
	TRISC2 = 0;
	TMR1 = 0;
	T1CON = 0x20;
	CCP1CON = 0x02;
	
	CCPR1 = 3110;	//400Hz
	CCP1IF = 0;
	CCP1IE = 1;
	TMR1ON = 0;
}

void BEEP ( void )
{
	if ( !count3 )
	{
		count3 = BEEP_LONG; 
		Beep = 1; 
		CCP4CON = 0x02;
	}
	Ext_Display_Update();
}

void BEEP_ERROR( void )
{
	count3 = ERROR_LONG; 
	Beep = 1; 
	CCP4CON = 0x02;
	Ext_Display_Update();
}
#endif


/* void SoundTest ( void ) */
/* { */
/* 	static unsigned long cnt = 0; */

/* 	if ( ++cnt == 500000 ) */
/* 	{ */
/* 		RingSound = 1; */
/* 		CCP4CON = 0x02;	  */
/* 		cnt = 0; 			 */
/* 	} */
/* 	else */
/* 		if ( cnt == 100000 ) */
/* 		{	 */
/* 			Beep = 1; */
/* 			CCP4CON = 0x02;	 */
/* 		} */
/* 		else */
/* 			if ( cnt == 200000 ) */
/* 			{	 */
/* 				Beep = 1; */
/* 				CCP4CON = 0x02;	 */
/* 			} */
/* } */
